package servlet.assitant;

import dao.domain.Book;
import service.AssistantService;
import service.BookService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author huangzhiyang
 * @Date 2023/10/16
 * @Description ${PACKAGE_name}
 */
@WebServlet(name = "BookDeleteServlet", value = "/BookDeleteServlet")
public class AssistantDeleteBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int number = Integer.parseInt(request.getParameter("number"));
        String msg = "{\"msg\":\"fail\"}";
        try {
            Book book = BookService.selectById(number);
            boolean flag = AssistantService.deleteBook(book);
            if (flag) {
                msg = "{\"msg\":\"success\"}";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        response.getWriter().write(msg);
        System.out.println("finish");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("BookDeleteServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
